package com.cskaoyan.controller.wechat;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.promotion.Coupon;
import com.cskaoyan.bean.promotion.CouponUser;
import com.cskaoyan.bean.user.MallToken;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.utils.MD5Utils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengww
 * @create 2020/5/5 7:27
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponService couponService;

    @GetMapping("list")
    public BaseRespVo getCouponList(Integer page, Integer size) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Coupon> list = couponService.queryCouponUserList4Wx(page,size,user.getId());
        Map<String, Object> result = new HashMap<>();
        long total = PageInfo.of(list).getTotal();
        result.put("count", total);
        result.put("data", list);
        return new BaseRespVo<>(0, result, "成功");
    }

    @GetMapping("mylist")
    public BaseRespVo getCouponList(Integer status, Integer page, Integer size) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Coupon> coupon = couponService.queryCouponUser4Wx(status, page, size,user.getId());
        Map<String, Object> result = new HashMap<>();
        long total = PageInfo.of(coupon).getTotal();
        result.put("count", total);
        result.put("data", coupon);
        return new BaseRespVo<>(0, result, "成功");
    }

    @GetMapping("selectlist")
    public BaseRespVo getCouponList(Integer cartId) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Coupon> list = couponService.selectCouponList(cartId,user.getId());
        return new BaseRespVo<>(0, list, "成功");
    }

    @PostMapping("receive")
    public BaseRespVo createCouponUser(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        couponService.createCouponUser((Integer)map.get("couponId"),user.getId());
        return new BaseRespVo<>(0, null, "成功");
    }

    @PostMapping("exchange")
    public BaseRespVo updateCoupon(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        couponService.createCouponUserByCode(map.get("code")+"",user.getId());
        return new BaseRespVo<>(0, null, "成功");
    }
}
